import { Component } from '@angular/core';
import { NavController, NavParams, ViewController } from 'ionic-angular';
import { HttpDataProvider } from '../../providers/http-data/http-data';
import { Task } from '../tasklist/task';

/**
 * Generated class for the InputTaskPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-input-task',
  templateUrl: 'input-task.html',
})
export class InputTaskPage {
  
  title: string;
  status: string='open';
  description: string;
  objectId: string;
  //数据,四个量
  
  action: string='新增';

  constructor(public navCtrl: NavController, 
    public navParams: NavParams,
    public viewCtrl: ViewController,
    public http: HttpDataProvider
    //控制的功能
    ) {
  }

  ionViewDidLoad() {
    // console.log('ionViewDidLoad InputTaskPage');
    let task: Task = this.navParams.get("task");//获取task，判断是否为真
    if(task){
      this.title = task.title;
      this.status = task.status;
      this.description = task.description;
      this.objectId = task.objectId;
      this.action = '编辑';
      
    }
  }

  close(){
     this.viewCtrl.dismiss();

  }

    saveTask(){
      let _task : Task={
         title: this.title,
         status: this.status,
         description: this.description,
         objectId: this.objectId
      };
      if(this.action === '新增'){
        this.http.addTask(_task).subscribe(data=>_task.objectId = data.objectId);
      }
      else if(this.action === '编辑'){
         this.http.updateTask(_task).subscribe();

      }
      this.viewCtrl.dismiss(_task);//可以关闭提示页面的代码
    }
}
